package com.a.b.c.d.e.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created on 2021/12/24.
 *
 * @author yanshuchun
 */
@RestController
@RequestMapping("/activiti")
public class ActivitiController {
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private DefaultProcessDiagramGenerator defaultProcessDiagramGenerator;
    @GetMapping("/deploy")
    public void deploy(){
        Deployment deploy = repositoryService.createDeployment().addClasspathResource("processes/my.bpmn").deploy();
        repositoryService.createProcessDefinitionQuery().deploymentId(deploy.getId()).singleResult();
    }
    @GetMapping("/start/{id}")
    public void start(@PathVariable("id")String id){
        Map paramMap = new HashMap();
        paramMap.put("userId","6688");
        DeploymentQuery deploymentQuery = repositoryService.createDeploymentQuery().deploymentKey("Process_1wlbmwm");
        System.out.println(deploymentQuery);
        String bussinessId = "1";
        runtimeService.startProcessInstanceById(id,bussinessId,paramMap);
    }
    @GetMapping("/queryMyTask/{userId}")
    public void queryMyTask(@PathVariable("userId") String userId){
        List<Task> taskList  = taskService.createTaskQuery().taskAssignee(userId).list();
        taskList.stream().forEach(item->{
            System.out.println(item.getId());
        });
    }
    @GetMapping("/queryMyTaskDetail/{taskId}")
    public void queryMyTaskDetail(@PathVariable("taskId") String taskId){
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        String processInstanceId = task.getProcessInstanceId();
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        String businessKey = processInstance.getBusinessKey();
        System.out.println(businessKey);
    }
    @GetMapping("/doMyTask/{taskId}")
    public void doMyTask(@PathVariable("taskId") String taskId){
        Map param = new HashMap();
        param.put("userId","6689");
        param.put("leaderFlag",1);
        taskService.complete(taskId,param);
    }
    @GetMapping("/getPic/{taskId}")
    public void getPic(@PathVariable("taskId") String taskId, HttpServletResponse response){
        try {
            Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
            String processInstanceId = task.getProcessDefinitionId();
            BpmnModel bpmnModel = repositoryService.getBpmnModel(processInstanceId);
            List<String> activeActivityIds = runtimeService.getActiveActivityIds(task.getProcessInstanceId());
            InputStream in = defaultProcessDiagramGenerator.generateDiagram(bpmnModel, "png",activeActivityIds);
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            int ch;
            while ((ch = in.read()) != -1) {
                bout.write(ch);
            }
            OutputStream out = response.getOutputStream();
            out.write(bout.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

}
